package cn.fl.riskctrl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.service.IRskCtrlMDService;
import cn.fl.riskctrl.transform.RskCtrlDVOTransform;
import cn.fl.riskctrl.transform.RskCtrlMVOTransform;
import cn.fl.riskctrl.vo.RskCtrlDVO;
import cn.fl.riskctrl.vo.RskCtrlMVO;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlAddApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlModifyApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlRemoveApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlSearchApiService;
import fl.riskctrl.facade.riskctrl.condition.RskCtrlDQC;
import fl.riskctrl.facade.riskctrl.dto.RskCtrlDDTO;
import fl.riskctrl.facade.riskctrl.dto.RskCtrlMDTO;

/**
 * Created by Administrator on 2017/6/5.
 */
@Service
public class RskCtrlMDServiceImpl implements IRskCtrlMDService {

	private static final Logger logger = LoggerFactory.getLogger(RskCtrlMDServiceImpl.class);

    @Autowired
    private IRiskCtrlSearchApiService riskCtrlSearchApiService;

    @Autowired
    private IRiskCtrlAddApiService riskCtrlAddApiService;

    @Autowired
    private IRiskCtrlRemoveApiService riskCtrlRemoveApiService;

    @Autowired
    private IRiskCtrlModifyApiService riskCtrlModifyApiService;

    @Autowired
    private ISysDictionaryService sysDictionaryService;

    @Override
    public Pager searchListPage(Pager page) {
        if(null == page)page = new Pager();
        Pager pager = this.riskCtrlSearchApiService.searchRiskCtrlMListPage(page);
        List<RskCtrlMDTO> resultList = (List<RskCtrlMDTO>) pager.getResultList();
        //数据字典（是否启用）
        List<String> codeGrpCdList = new ArrayList<>();
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, String> statusMap = null;
        if (CollectionUtils.isNotEmpty(statusList)) {
            statusMap = new HashMap<>(statusList.size());
            for (KeyValueVO dto : statusList) {
                statusMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<RskCtrlMVO> rskCtrlCfgMVOList = RskCtrlMVOTransform.toVOList(resultList);
        for (RskCtrlMVO rskCtrlMVO : rskCtrlCfgMVOList) {
            if (statusMap != null && statusMap.containsKey(String.valueOf(rskCtrlMVO.getIsValid()))) {
                rskCtrlMVO.setIsOpen(statusMap.get(String.valueOf(rskCtrlMVO.getIsValid())));
            }
        }
        pager.setResultList(rskCtrlCfgMVOList);
        return pager;
    }

    @Override
    public List<RskCtrlDVO> searchListSon(RskCtrlDQC qc) {
        if(null == qc)qc = new RskCtrlDQC();
        List<RskCtrlDDTO> rskCtrlDDTOList = this.riskCtrlSearchApiService.searchRiskCtrlDList(qc);
        //数据字典（是否启用）
        List<String> codeGrpCdList = new ArrayList<>();
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, String> statusMap = null;
        if (CollectionUtils.isNotEmpty(statusList)) {
            statusMap = new HashMap<>(statusList.size());
            for (KeyValueVO dto : statusList) {
                statusMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<RskCtrlDVO> rskCtrlCfgMVOList = RskCtrlDVOTransform.toVOList(rskCtrlDDTOList);
        for (RskCtrlDVO rskCtrlDVO : rskCtrlCfgMVOList) {
            if (statusMap != null && statusMap.containsKey(String.valueOf(rskCtrlDVO.getIsValid()))) {
                rskCtrlDVO.setIsOpen(statusMap.get(String.valueOf(rskCtrlDVO.getIsValid())));
            }
        }
        return rskCtrlCfgMVOList;
    }

    @Override
    public Long add(RskCtrlMVO entity) {
        if(null == entity){
            BaseLogUtils.info(logger, "add", "新增风控管理主表信息失败,风控管理主表信息对象参数为空.DATA:"+ JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlAddApiService.addRiskCtrlM(RskCtrlMVOTransform.toDTO(entity));
    }

    @Override
    public Long add(RskCtrlDVO entity) {
        if(null == entity){
            BaseLogUtils.info(logger, "add", "新增风控管理子表信息失败,风控管理子表信息对象参数为空.DATA:"+ JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlAddApiService.addRiskCtrlD(RskCtrlDVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modify(RskCtrlMVO entity) {
        if(null == entity){
            BaseLogUtils.info(logger, "modify", "修改风控管理主表信息失败,风控管理主表信息对象参数为空.DATA:"+ JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRiskCtrlM(RskCtrlMVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modify(RskCtrlDVO entity) {
        if(null == entity){
            BaseLogUtils.info(logger, "modify", "修改风控管理子表信息失败,风控管理子表信息对象参数为空.DATA:"+ JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRiskCtrlD(RskCtrlDVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modifyRMIsvalid(Long id, int isValid) {
        if(null == id){
            BaseLogUtils.info(logger, "modifyRMIsvalid", "修改风控管理主表状态失败,风控管理子表对象参数为空.DATA:"+ id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRMIsValid(id, isValid);
    }

    @Override
    public Boolean modifyRDIsvalid(Long id, int isValid) {
        if(null == id){
            BaseLogUtils.info(logger, "modifyRDIsvalid", "修改风控管理子表状态失败,风控管理子表对象参数为空.DATA:"+ id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRDIsValid(id, isValid);
    }

    @Override
    public Boolean removeById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "removeById", "删除风控管理主表信息失败,风控管理主表信息对象参数为空.DATA:"+ JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlRemoveApiService.removeRiskCtrlMById(id);
    }

    @Override
    public Boolean removeSONById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "removeSONById", "删除风控管理子表信息失败,风控管理子表信息对象参数为空.DATA:"+ JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlRemoveApiService.removeRiskCtrlDById(id);
    }

    @Override
    public RskCtrlMVO selectById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "selectById", "修改风控管理主表信息时查询一条数据失败,风控管理主表信息对象参数为空.DATA:"+ JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        RskCtrlMDTO dto = this.riskCtrlSearchApiService.selectRiskCtrlMById(id);
        return RskCtrlMVOTransform.toVO(dto);
    }

    @Override
    public RskCtrlDVO selectSONById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "selectSONById", "修改风控管理子表信息时查询一条数据失败,风控管理子表信息对象参数为空.DATA:"+ JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        RskCtrlDDTO dto = this.riskCtrlSearchApiService.selectRiskCtrlDById(id);
        return RskCtrlDVOTransform.toVO(dto);
    }
}
